import java.io.*;
import java.util.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.lang.System.out;

public class  PcClient {
    String uname;
    PrintWriter printWriter;
    BufferedReader bufferReader;
    Socket client;
    MessagesThread myThread;
    
    public PcClient(String uname,String servername) throws Exception {
        this.uname = uname;
        client  = new Socket(servername,2046);
        bufferReader = new BufferedReader( new InputStreamReader( client.getInputStream()) ) ;
        printWriter = new PrintWriter(client.getOutputStream(),true);
        printWriter.println("computer " + uname);  // send name to server
        myThread = new MessagesThread();
        myThread.start();  // create thread for listening for messages
     
    }


    public static void main(String ... args) {
    	System.out.print("User Name: ");
        Scanner stdin = new Scanner( System.in );
    	// take username from user
        String name = stdin.nextLine();
    	/*
    	ClientLogin login = new ClientLogin();
    	String name="";
    	String password="";
    	while(name==""&&password==""){
    		name=login.getUsername();
    		password=login.getPassword();
    	}
    	System.out.println(name);
    	System.out.println(password);
        */
    	System.out.println("Connecting...");
        String servername = "lore.cs.purdue.edu";
        
        try {
            new PcClient( name ,servername);
        } catch(Exception ex) {
            out.println( "Error --> " + ex.getMessage());
        }
        

    } // end of main

    // Check if the pc client thread is alive
    public boolean isAlive()
    {
    	return myThread.isAlive();
    }
    
    // Terminate the pc client thread
    public void terminate()
    {
    	myThread.terminate();
    	printWriter.println("QUIT");
    }
    
    public int getScale()
    {
    	return myThread.getScale();
    }
    
    public void setScale( int n )
    {
    	myThread.setScale(n);
    }
    
    // inner class for Messages Thread
    class  MessagesThread extends Thread {
        Robot myRobot;
        int scale=10;
        Toolkit toolkit =  Toolkit.getDefaultToolkit ();
        Dimension dim = toolkit.getScreenSize();
        int screen_width = dim.width;
        int screen_height = dim.height;
        
        private volatile Boolean ifStop = false;
        
        // Function to terminate the current thread
        public void terminate() {
            ifStop = true;
        }
        public int getScale()
        {
        	return scale;
        }
        
        public void setScale( int n )
        {
        	this.scale = n;
        }
        // Communication protocol
        // "mouse move x y" ( x, y could be positive or negative, meaning increasing or decreaseing )
        // "mouse press x" (0 is left key, 1 is right key, 2 is middle key)
        // "mouse release x" (0 is left key, 1 is right key, 2 is middle key)
        // "keyboard press x" (x is ASCII code)
        // "keyboard press x" (x is ASCII code)
        
    	  void processCmd(String cmd)
    	    {
    		  System.out.println("1");
    	    	String newCmd [] = cmd.split(" ");
    	    	if( newCmd[0].equals("mouse"))
    	    	{
    	    		 System.out.println("2");
    	    		if( newCmd[1].equals("press") )
    	    		{
    	    			int btn = Integer.parseInt( newCmd[2] );
    	    			switch(btn)
    	    			{	
    	    				case 0: myRobot.mousePress(InputEvent.BUTTON1_MASK);
    	    						break;
    	    				case 1: myRobot.mousePress(InputEvent.BUTTON2_MASK);
    								break;		
    	    				case 2: myRobot.mousePress(InputEvent.BUTTON3_MASK);
    								break;
    	    			}
    	    		}
    	    		else if( newCmd[1].equals("release"))
    	    		{
    	    			int btn = Integer.parseInt( newCmd[2] );
    	    			switch(btn)
    	    			{
    	    				case 0: myRobot.mouseRelease(InputEvent.BUTTON1_MASK);
    	    						break;
    	    				case 1: myRobot.mouseRelease(InputEvent.BUTTON2_MASK);
    								break;		
    	    				case 2: myRobot.mouseRelease(InputEvent.BUTTON3_MASK);
    								break;
    	    			}

    	    		}
    	    		else if( newCmd[1].equals("move"))
    	    		{
    	    			 System.out.println("3");
    	    			int x = Integer.parseInt( newCmd[2] );
    	    			int y = Integer.parseInt( newCmd[3] );
    	    			
    	    			int current_x=MouseInfo.getPointerInfo().getLocation().x;
    	    			int current_y=MouseInfo.getPointerInfo().getLocation().y;
    	    			x = current_x + x*scale;
    	    			y = current_y + y*scale;
    	    			if( x < 0 ) x = 0;
    	    			else if( x > screen_width ) x = screen_width; 
    	    			if( y < 0 ) y = 0;
    	    			else if( y > screen_height ) y = screen_height; 
    	    			myRobot.mouseMove( x , y );
    	    			 System.out.println("4");
    	    		}
    	    	}
    	    	else if( newCmd[0].equals("keyboard") )
    	    	{
    	    		if( newCmd[1].equals("press") )
    	    		{
    	    			int key = Integer.parseInt( newCmd[2] );
    	    			myRobot.keyPress(key);
    	    		}
    	    		else if( newCmd[1].equals("release"))
    	    		{
    	    			int key = Integer.parseInt( newCmd[2] );
    	    			myRobot.keyRelease(key);
    	    		}
    	    		
    	    	}
    	    }
        public void run() {
        	try{
        		myRobot = new Robot();
        	}catch(Exception e){        		
        	}
            String line;
            Thread thisThread = Thread.currentThread();
            try {
                while(ifStop==false) {
                    line = bufferReader.readLine();
                    if( line != null )
                    {
                      processCmd(line);
                      System.out.println(line + "\n");
                    }
                } // end of while
            } catch(Exception ex) {}
            return;
        }
    }
} //  end of client
